// Zoop Baddy by Andrew Davies

"use strict";

function ZoopBaddy( totalboardPosX, totalboardPosY, colour, dir, animSpd, frameIdx )
{
	this.colour = colour;
	this.dir = dir;
	this.state = "normal";
	
	// normal
	
	// dead
	this.deadFrameIdx = 0;	
	this.deadMaxFrames = 30;
		
	this.sprite = new Sprite( "Sprites/InGame/allMushSprites.png", animSpd, frameIdx );
	this.spriteAnims = new Array();	
	this.spriteAnims[ "blue" ] = new Array();		
	this.spriteAnims[ "red" ] = new Array();		
	this.spriteAnims[ "yellow" ] = new Array();	
	this.spriteAnims[ "green" ] = new Array();
	
	var blueDownSpriteAnim = new Array();
	blueDownSpriteAnim.push( new SpriteFrame( 32 * 3,	32 * 4, 32, 32 ) );
	blueDownSpriteAnim.push( new SpriteFrame( 32 * 4,	32 * 4, 32, 32 ) );
	blueDownSpriteAnim.push( new SpriteFrame( 32 * 5,	32 * 4, 32, 32 ) );	
	this.spriteAnims[ "blue" ][ "down" ] = blueDownSpriteAnim;
	
	var blueLeftSpriteAnim = new Array();
	blueLeftSpriteAnim.push( new SpriteFrame( 32 * 3,	32 * 5, 32, 32 ) );
	blueLeftSpriteAnim.push( new SpriteFrame( 32 * 4,	32 * 5, 32, 32 ) );
	blueLeftSpriteAnim.push( new SpriteFrame( 32 * 5,	32 * 5, 32, 32 ) );	
	this.spriteAnims[ "blue" ][ "left" ] = blueLeftSpriteAnim;
	
	var blueRightSpriteAnim = new Array();
	blueRightSpriteAnim.push( new SpriteFrame( 32 * 3,	32 * 6, 32, 32 ) );
	blueRightSpriteAnim.push( new SpriteFrame( 32 * 4,	32 * 6, 32, 32 ) );
	blueRightSpriteAnim.push( new SpriteFrame( 32 * 5,	32 * 6, 32, 32 ) );	
	this.spriteAnims[ "blue" ][ "right" ] = blueRightSpriteAnim;	
	
	var blueUpSpriteAnim = new Array();
	blueUpSpriteAnim.push( new SpriteFrame( 32 * 3,		32 * 7, 32, 32 ) );
	blueUpSpriteAnim.push( new SpriteFrame( 32 * 4,		32 * 7, 32, 32 ) );
	blueUpSpriteAnim.push( new SpriteFrame( 32 * 5,		32 * 7, 32, 32 ) );	
	this.spriteAnims[ "blue" ][ "up" ] = blueUpSpriteAnim;	
	
	var greenDownSpriteAnim = new Array();
	greenDownSpriteAnim.push( new SpriteFrame( 32 * 6,	32 * 4, 32, 32 ) );
	greenDownSpriteAnim.push( new SpriteFrame( 32 * 7,	32 * 4, 32, 32 ) );
	greenDownSpriteAnim.push( new SpriteFrame( 32 * 8,	32 * 4, 32, 32 ) );	
	this.spriteAnims[ "green" ][ "down" ] = greenDownSpriteAnim;
	
	var greenLeftSpriteAnim = new Array();
	greenLeftSpriteAnim.push( new SpriteFrame( 32 * 6,	32 * 5, 32, 32 ) );
	greenLeftSpriteAnim.push( new SpriteFrame( 32 * 7,	32 * 5, 32, 32 ) );
	greenLeftSpriteAnim.push( new SpriteFrame( 32 * 8,	32 * 5, 32, 32 ) );	
	this.spriteAnims[ "green" ][ "left" ] = greenLeftSpriteAnim;
	
	var greenRightSpriteAnim = new Array();
	greenRightSpriteAnim.push( new SpriteFrame( 32 * 6,	32 * 6, 32, 32 ) );
	greenRightSpriteAnim.push( new SpriteFrame( 32 * 7,	32 * 6, 32, 32 ) );
	greenRightSpriteAnim.push( new SpriteFrame( 32 * 8,	32 * 6, 32, 32 ) );	
	this.spriteAnims[ "green" ][ "right" ] = greenRightSpriteAnim;	
	
	var greenUpSpriteAnim = new Array();
	greenUpSpriteAnim.push( new SpriteFrame( 32 * 6,		32 * 7, 32, 32 ) );
	greenUpSpriteAnim.push( new SpriteFrame( 32 * 7,		32 * 7, 32, 32 ) );
	greenUpSpriteAnim.push( new SpriteFrame( 32 * 8,		32 * 7, 32, 32 ) );	
	this.spriteAnims[ "green" ][ "up" ] = greenUpSpriteAnim;	
		
	var redDownSpriteAnim = new Array();
	redDownSpriteAnim.push( new SpriteFrame( 32 * 9,		32 * 4, 32, 32 ) );
	redDownSpriteAnim.push( new SpriteFrame( 32 * 10,		32 * 4, 32, 32 ) );
	redDownSpriteAnim.push( new SpriteFrame( 32 * 11,		32 * 4, 32, 32 ) );	
	this.spriteAnims[ "red" ][ "down" ] = redDownSpriteAnim;
	
	var redLeftSpriteAnim = new Array();
	redLeftSpriteAnim.push( new SpriteFrame( 32 * 9,		32 * 5, 32, 32 ) );
	redLeftSpriteAnim.push( new SpriteFrame( 32 * 10,		32 * 5, 32, 32 ) );
	redLeftSpriteAnim.push( new SpriteFrame( 32 * 11,		32 * 5, 32, 32 ) );	
	this.spriteAnims[ "red" ][ "left" ] = redLeftSpriteAnim;
	
	var redRightSpriteAnim = new Array();
	redRightSpriteAnim.push( new SpriteFrame( 32 * 9,	32 * 6, 32, 32 ) );
	redRightSpriteAnim.push( new SpriteFrame( 32 * 10,	32 * 6, 32, 32 ) );
	redRightSpriteAnim.push( new SpriteFrame( 32 * 11,	32 * 6, 32, 32 ) );	
	this.spriteAnims[ "red" ][ "right" ] = redRightSpriteAnim;	
	
	var redUpSpriteAnim = new Array();
	redUpSpriteAnim.push( new SpriteFrame( 32 * 9,	32 * 7, 32, 32 ) );
	redUpSpriteAnim.push( new SpriteFrame( 32 * 10,	32 * 7, 32, 32 ) );
	redUpSpriteAnim.push( new SpriteFrame( 32 * 11,	32 * 7, 32, 32 ) );	
	this.spriteAnims[ "red" ][ "up" ] = redUpSpriteAnim;	
		
	var yellowDownSpriteAnim = new Array();
	yellowDownSpriteAnim.push( new SpriteFrame( 32 * 3,		32 * 0, 32, 32 ) );
	yellowDownSpriteAnim.push( new SpriteFrame( 32 * 4,		32 * 0, 32, 32 ) );
	yellowDownSpriteAnim.push( new SpriteFrame( 32 * 5,		32 * 0, 32, 32 ) );	
	this.spriteAnims[ "yellow" ][ "down" ] = yellowDownSpriteAnim;
	
	var yellowLeftSpriteAnim = new Array();
	yellowLeftSpriteAnim.push( new SpriteFrame( 32 * 3,		32 * 1, 32, 32 ) );
	yellowLeftSpriteAnim.push( new SpriteFrame( 32 * 4,		32 * 1, 32, 32 ) );
	yellowLeftSpriteAnim.push( new SpriteFrame( 32 * 5,		32 * 1, 32, 32 ) );	
	this.spriteAnims[ "yellow" ][ "left" ] = yellowLeftSpriteAnim;
	
	var yellowRightSpriteAnim = new Array();
	yellowRightSpriteAnim.push( new SpriteFrame( 32 * 3,	32 * 2, 32, 32 ) );
	yellowRightSpriteAnim.push( new SpriteFrame( 32 * 4,	32 * 2, 32, 32 ) );
	yellowRightSpriteAnim.push( new SpriteFrame( 32 * 5,	32 * 2, 32, 32 ) );	
	this.spriteAnims[ "yellow" ][ "right" ] = yellowRightSpriteAnim;	
	
	var yellowUpSpriteAnim = new Array();
	yellowUpSpriteAnim.push( new SpriteFrame( 32 * 3,	32 * 3, 32, 32 ) );
	yellowUpSpriteAnim.push( new SpriteFrame( 32 * 4,	32 * 3, 32, 32 ) );
	yellowUpSpriteAnim.push( new SpriteFrame( 32 * 5,	32 * 3, 32, 32 ) );	
	this.spriteAnims[ "yellow" ][ "up" ] = yellowUpSpriteAnim;	

	this.totalboardPos = new Vec2( totalboardPosX, totalboardPosY );
	
	this.boundingBox = function( topLeft, bottomRight )
	{
		topLeft.x = ( this.totalboardPos.x * BOARD_UNIT_SIZE_X );
		topLeft.y = ( this.totalboardPos.y * BOARD_UNIT_SIZE_Y );
		bottomRight.x = topLeft.x + BOARD_UNIT_SIZE_X;
		bottomRight.y = topLeft.y + BOARD_UNIT_SIZE_Y;
	}	
	
	this.move = function( dir )
	{
		vec2AddV( this.totalboardPos, dir );
	}
	
	this.kill = function( frameIdx )
	{	
		this.deadFrameIdx = frameIdx;
		this.state = "dead";
	}	
	
	this.totallyDead = function( frameIdx )
	{			
		if( this.state == "dead" )
		{
			var numFramesDead = frameIdx - this.deadFrameIdx;
			return ( numFramesDead >= this.deadMaxFrames );
		}
		
		return false;
	}		
	
	this.update = function()
	{
	
		this.sprite.update();	
	}
		
	this.draw = function( frameIdx )
	{	
		var centerPos = new Vec2( 0, 0 );
		vec2rvMulV( centerPos, this.totalboardPos, g_boardUnitSize )
		vec2AddV( centerPos, g_halfBoardUnitSize );
		
		var size = vec2Assign( g_boardUnitSize );
		var alpha = 1.0;
		
		switch( this.state )
		{
		
		case "normal":
		{
		}
		break;
		
		case "dead":
		{
			var numFramesDead = frameIdx - this.deadFrameIdx;
			var scale = 1.0 + ( numFramesDead * 0.4 );
			vec2MulS( size, scale );
			
			alpha = ( this.deadMaxFrames - numFramesDead ) / this.deadMaxFrames;	
		}
		break;
		
		}
		
		var halfSize = vec2AssignDivS( size, 2 );
		var topLeft = vec2AssignSubV( centerPos, halfSize );
		var bottomRight = vec2AssignAddV( centerPos, halfSize );		
		
		this.sprite.draw( 
			this.spriteAnims[ this.colour ][ this.dir ], 
			topLeft.x, topLeft.y,
			size.x, size.y,
			alpha,
			frameIdx );	
	}
	
}
